package org.example.lottery_system.common.handler;

import org.example.lottery_system.common.errorcode.ControllerErrorCodeConstants;
import org.example.lottery_system.common.errorcode.ErrorCode;
import org.example.lottery_system.common.errorcode.GlobalErrorCodeConstants;
import org.example.lottery_system.common.errorcode.ServiceErrorCodeConstants;
import org.example.lottery_system.common.exception.ControllerException;
import org.example.lottery_system.common.exception.ServiceException;
import org.example.lottery_system.common.pojo.CommonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<String> controllerException(ServiceException e){
        return CommonResult.error(ControllerErrorCodeConstants.REGISTER_ERROR.getCode(),e.getMessage());
    }
    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<String> serviceException(ControllerException e){
        return CommonResult.error(ServiceErrorCodeConstants.PHONENUMBER_USED_ERROR.getCode(),e.getMessage());
    }
    @ExceptionHandler
    public CommonResult<String> exception(Exception e){
        return CommonResult.error(GlobalErrorCodeConstants.ERROR.getCode(),e.getMessage());
    }
}
